package com.tchintech.pay.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.BankAddressAdapter;
import com.tchintech.pay.delegate.BankAddressDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.BankAdress;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/25 0025.\
 * 选择详细地址
 */

public class BankAddressActivity extends ActivityPresenter<BankAddressDelegate> implements View.OnClickListener {
    private ListView listView;
    private BankAddressAdapter bankAddressAdapter;
    private List<BankAdress> list;
    private String Url_bankadress = "Bound.queryAdress";
    private Map<String, String> map;
    private Handler handler;
    private String bankname = "";
    private String city = "";
    private String pro = "";

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTv_title("银行地址");
        viewDelegate.setOnClickListener(this, R.id.iv_back);
        listView = viewDelegate.getLv_bankad();
        bankAddressAdapter = new BankAddressAdapter(this, list);
        listView.setAdapter(bankAddressAdapter);
        handler();
        queryPro();
        select();
    }

    @Override
    protected void initData() {
        super.initData();
        list = new ArrayList<BankAdress>();
        bankname = getIntent().getStringExtra("bankname");
        city = getIntent().getStringExtra("city");
        pro = getIntent().getStringExtra("pro");
    }


    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.QUERY_ADDRESS:
                        try {
                            JSONObject jsonObject = new JSONObject(msg.obj.toString());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                BankAdress bankAdress = new BankAdress();
                                JSONObject json = jsonArray.getJSONObject(i);
                                bankAdress.setName(jsonArray.getJSONObject(i).getString("BANK_NAME"));
                                bankAdress.setBank_no(jsonArray.getJSONObject(i).getString("BANK_CHANNEL_NO"));
                                list.add(bankAdress);
                            }
                            bankAddressAdapter.setList(list);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
    }

    /**
     * 查询省
     */
    private void queryPro() {
        map = new HashMap<String, String>();
        map.put("pro", pro);
        map.put("city", city);
        map.put("bank_name", bankname);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, Url_bankadress, map, HttpCode.QUERY_ADDRESS);
    }

    /**
     * 选择省
     */
    private void select() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BankAdress bankAdress = (BankAdress) parent.getItemAtPosition(position);
                String address = bankAdress.getName();
                String bank_number = bankAdress.getBank_no();
                //setResult(name,RESULT_OK);
                Intent intent = new Intent();
                intent.putExtra("address", address);
                intent.putExtra("bank_number", bank_number);
                intent.setClass(BankAddressActivity.this, BankCityActivity.class);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    protected Class<BankAddressDelegate> getDelegateClass() {
        return BankAddressDelegate.class;
    }
}